package com.xinfan.tank;

import java.util.Vector;

/**
 * @Author xin
 * @Date 2022/6/1 8:37
 * @Version 1.0
 */
public class EnemyTank extends Tank implements Runnable {

    //存储多个子弹
    //CopyOnWriteArrayList<com.xinfan.tank.Bullet> bullets = new CopyOnWriteArrayList<>();
    //Vector<com.xinfan.tank.Bullet> bullets = new Vector<>();

    //Boolean isAlive = true;

    //用于循环比较是否碰撞
    private Vector<EnemyTank> enemyTanks = new Vector<>();

    public void setEnemyTanks(Vector<EnemyTank> enemyTanks) {
        this.enemyTanks = enemyTanks;
    }

    public EnemyTank(int x, int y) {
        super(x, y);
    }

    public boolean isTouch() {
        for (int i = 0; i < enemyTanks.size(); i++) {
            EnemyTank enemyTank = enemyTanks.get(i);

            switch (this.getDirection()) {


                case 0:
                    //该坦克向上 其他敌人坦克向上/下 或者左/右 分两种情况讨论
                    //该坦克左上角坐标this.getX()，this.getY() 右上角坐标this.getX()，this.getY()+40
                    //敌人坦克上下方向时  x坐标范围[enemyTank.getX(),enemyTank.getX()+40] y坐标范围[enemyTank.getY(),enemyTank.getY()+60]
                    //不能和自己比较
                    if (this != enemyTank){
                        if (this.getX() >= enemyTank.getX()
                                && this.getX() <= enemyTank.getX() + 40
                                && this.getY() >= enemyTank.getY()
                                && this.getY() <= enemyTank.getY() + 60) {
                            return true;
                        }
                        if (this.getX() >= enemyTank.getX()
                                && this.getX() <= enemyTank.getX() + 40
                                && this.getY() + 40 >= enemyTank.getY()
                                && this.getY() + 40 <= enemyTank.getY() + 60) {
                            return true;
                        }
                        //敌人坦克左右方向时  x坐标范围[enemyTank.getX(),enemyTank.getX()+60] y坐标范围[enemyTank.getY(),enemyTank.getY()+40]
                        if (this.getX() >= enemyTank.getX()
                                && this.getX() <= enemyTank.getX() + 60
                                && this.getY() >= enemyTank.getY()
                                && this.getY() <= enemyTank.getY() + 40) {
                            return true;
                        }
                        if (this.getX() >= enemyTank.getX()
                                && this.getX() <= enemyTank.getX() + 60
                                && getY() + 40 >= enemyTank.getY()
                                && getY() + 40 <= enemyTank.getY() + 40) {
                            return true;
                        }
                        break;
                    }

                case 1:
                    //该坦克向右 其他敌人坦克向上/下 或者左/右 分两种情况讨论
                    //该坦克右上角坐标this.getX()+60，this.getY() 右下角坐标this.getX()+60，this.getY()+40
                    //敌人坦克上下方向时  x坐标范围[enemyTank.getX(),enemyTank.getX()+40] y坐标范围[enemyTank.getY(),enemyTank.getY()+60]
                    if (this != enemyTank) {
                        if (this.getX() + 60 >= enemyTank.getX()
                                && this.getX() + 60 <= enemyTank.getX() + 40
                                && this.getY() >= enemyTank.getY()
                                && this.getY() <= enemyTank.getY() + 60) {
                            return true;
                        }
                        if (this.getX() + 60 >= enemyTank.getX()
                                && this.getX() + 60 <= enemyTank.getX() + 40
                                && this.getY() + 40 >= enemyTank.getY()
                                && this.getY() + 40 <= enemyTank.getY() + 60) {
                            return true;
                        }
                        //敌人坦克左右方向时  x坐标范围[enemyTank.getX(),enemyTank.getX()+60] y坐标范围[enemyTank.getY(),enemyTank.getY()+40]
                        if (this.getX() + 60 >= enemyTank.getX()
                                && this.getX() + 60 <= enemyTank.getX() + 60
                                && this.getY() >= enemyTank.getY()
                                && this.getY() <= enemyTank.getY() + 40) {
                            return true;
                        }
                        if (this.getX() + 60 >= enemyTank.getX()
                                && this.getX() + 60 <= enemyTank.getX() + 60
                                && this.getY() + 40 >= enemyTank.getY()
                                && this.getY() + 40 <= enemyTank.getY() + 40) {
                            return true;
                        }
                        break;
                    }
                case 2:
                    //该坦克向下 其他敌人坦克向上/下 或者左/右 分两种情况讨论
                    //该坦克左下角坐标this.getX()，this.getY()+60 右下角坐标this.getX()+40，this.getY()+60
                    //敌人坦克上下方向时  x坐标范围[enemyTank.getX(),enemyTank.getX()+40] y坐标范围[enemyTank.getY(),enemyTank.getY()+60]
                    if (this != enemyTank) {
                        if (this.getX() >= enemyTank.getX()
                                && this.getX() <= enemyTank.getX() + 40
                                && this.getY() + 60 >= enemyTank.getY()
                                && this.getY() + 60 <= enemyTank.getY() + 60) {
                            return true;
                        }
                        if (this.getX() + 40 >= enemyTank.getX()
                                && this.getX() + 40 <= enemyTank.getX() + 40
                                && this.getY() + 60 >= enemyTank.getY()
                                && this.getY() + 60 <= enemyTank.getY() + 60) {
                            return true;
                        }
                        //敌人坦克左右方向时  x坐标范围[enemyTank.getX(),enemyTank.getX()+60] y坐标范围[enemyTank.getY(),enemyTank.getY()+40]
                        if (this.getX() >= enemyTank.getX()
                                && this.getX() <= enemyTank.getX() + 60
                                && this.getY() + 60 >= enemyTank.getY()
                                && this.getY() + 60 <= enemyTank.getY() + 40) {
                            return true;
                        }
                        if (this.getX() + 40 >= enemyTank.getX()
                                && this.getX() + 40 <= enemyTank.getX() + 60
                                && this.getY() + 60 >= enemyTank.getY()
                                && this.getY() + 60 <= enemyTank.getY() + 40) {
                            return true;
                        }
                        break;
                    }
                case 3:
                    //该坦克向左 其他敌人坦克向上/下 或者左/右 分两种情况讨论
                    //该坦克左上角坐标this.getX()，this.getY() 左下角坐标this.getX()，this.getY()+40
                    //敌人坦克上下方向时  x坐标范围[enemyTank.getX(),enemyTank.getX()+40] y坐标范围[enemyTank.getY(),enemyTank.getY()+60]
                    if (this != enemyTank) {
                        if (this.getX() >= enemyTank.getX()
                                && this.getX() <= enemyTank.getX() + 40
                                && this.getY() >= enemyTank.getY()
                                && this.getY() <= enemyTank.getY() + 60) {
                            return true;
                        }
                        if (this.getX() >= enemyTank.getX()
                                && this.getX() <= enemyTank.getX() + 40
                                && this.getY() + 40 >= enemyTank.getY()
                                && this.getY() + 40 <= enemyTank.getY() + 60) {
                            return true;
                        }
                        //敌人坦克左右方向时  x坐标范围[enemyTank.getX(),enemyTank.getX()+60] y坐标范围[enemyTank.getY(),enemyTank.getY()+40]
                        if (this.getX() >= enemyTank.getX()
                                && this.getX() <= enemyTank.getX() + 60
                                && this.getY() >= enemyTank.getY()
                                && this.getY() <= enemyTank.getY() + 40) {
                            return true;
                        }
                        if (this.getX() >= enemyTank.getX()
                                && this.getX() <= enemyTank.getX() + 60
                                && this.getY() + 40 >= enemyTank.getY()
                                && this.getY() + 40 <= enemyTank.getY() + 40) {
                            return true;
                        }
                        break;
                    }

            }


        }
        return false;

    }

    public void move() {
        switch (getDirection()) {
            case 0:
                for (int i = 0; i < 30; i++) {
                    if (!isTouch()) {
                        moveUp();
                    }

                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                break;
            case 1:
                for (int i = 0; i < 30; i++) {
                    if (!isTouch()) {
                        moveRight();
                    }

                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                break;
            case 2:
                for (int i = 0; i < 30; i++) {
                    if (!isTouch()) {
                        moveDown();
                    }

                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                break;

            case 3:
                for (int i = 0; i < 30; i++) {
                    if (!isTouch()) {
                        moveLeft();
                    }

                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                break;
        }
    }


    @Override
    public void run() {
        while (true) {
            move();
            if (bullets.size() == 0) {
                shot();
            }

            setDirection((int) (Math.random() * 4));
            if (!isAlive) {
                break;
            }
        }

    }


}
